<template>
	<view>
		<image class="bgImage" src="/static/image/guide/homeBg.png" mode="widthFix"></image>
		<view class="navBox">
			<image class="backBtn" @click="goBack" src="http://cdn.yic3.cn/lawyer/mp-xhs/mine/backIcon.png"
				mode="widthFix"></image>
			<p>{{title}}</p>
		</view>
		<view class="" style="width: 100%;height: 188rpx;"></view>
		<scroll-view class="content" :scroll-y="true">

			<block v-for="(item,index) in Object.keys(uiData)" :key="index">
				<view class="inputBox" v-if="uiData[item].type == 'input'">
					<p>{{uiData[item].title}}</p>
					<input :placeholder="uiData[item].described" :disabled="item=='typeName'?true:false" type="text"
						v-model="formData[item]"
						:style="item=='typeName' &&'border: 2rpx solid rgba(255,255,255,0.4)'" />
				</view>
				<view class="inputBox" v-if="uiData[item].type == 'time'">
					<p>{{uiData[item].title}}</p>
					<view class="timeSelect" @click="selectTime(item)">
						<text v-if="formData[item]">{{formData[item]}}</text>
						<text v-else style="color: rgba(255,255,255,0.4);">{{uiData[item].described}}</text>
						<image src="https://cdn.yic3.cn/lawyer/mp-xhs/contract/time.png" mode=""></image>
					</view>
				</view>
				<view class="inputBox" v-if="uiData[item].type == 'textarea'">
					<p>{{uiData[item].title}}</p>
					<textarea name="" maxlength="100" v-model="formData[item]" :placeholder="uiData[item].described"
						id="" cols="30" rows="10"></textarea>
					<text>{{formData[item].length}}/100</text>
				</view>
			</block>
		</scroll-view>
		<view class="btnBox" @click="writeNow">
			确认提交
		</view>
		<u-popup :show="popShow" bgColor="transparent" mode="center">
			<view class="popBox">
				<p>内容描述</p>
				<scroll-view class="popScrollBox" scroll-y="true">
					<view v-for="(item,index) in Object.keys(formData)" :key="index">
						<p>{{uiData[item].title}}</p>
						<p>{{formData[item]}}</p>
					</view>
				</scroll-view>
				<view class="popBtn">
					<p @click="cancle">返回修改</p>
					<p @click="legalLitigation">确认撰写</p>
				</view>
			</view>
		</u-popup>
		<u-datetime-picker @cancel="timeShow = false" @confirm="timeConfirm" :show="timeShow" v-model="timeVal"
			mode="date"></u-datetime-picker>
	</view>
</template>

<script>
	import {
		typeFormData
	} from './data.js'
	export default {
		data() {
			return {
				title: '',
				popShow: false,
				typeId: '',
				formData: {},
				uiData: {},
				module: 1,
				timeVal: new Date().getTime(),
				timeShow: false,
				nowTimeKey: ''
			};
		},
		onLoad(e) {
			console.log(e)
			let type
			let obj = JSON.parse(e.item)
			this.module = e.module
			if (e.module==1) type = 'contractFormData'
			if (e.module==2) type = 'litigationFormData'
			if (e.module==3) type = 'fairFormData'
			if (e.module==4 && obj.id == 40) type = 'agreementData'
			if (e.module==4 && obj.id == 41) type = 'applicationData'
			this.formData = typeFormData[type].data
			this.uiData = typeFormData[type].uiData
			this.title = typeFormData[type].title
			this.formData.typeName = obj.name
			this.typeId = obj.id
		},
		methods: {
			writeNow() {
				if (Object.values(this.formData).some(item => !item)) return uni.showToast({
					title: '请把所有信息填写完成',
					icon: 'none'
				})
				this.popShow = true
			},
			cancle() {
				this.popShow = false
			},
			// 选择日期
			selectTime(e) {
				this.nowTimeKey = e
				this.timeVal = new Date(this.formData[e]).getTime() || new Date().getTime()
				this.timeShow = true
			},
			// 时间确认
			timeConfirm(e) {
				this.formData[this.nowTimeKey] = this.$u.timeFormat(e.value, 'yyyy-mm-dd')
				this.timeShow = false
			},
			legalLitigation() {
				uni.showLoading()
				this.popShow = false
				this.$api.legalLitigation({
					typeId: this.typeId - 0,
					module: this.module - 0,
					content: JSON.stringify(this.formData)
				}).then(res => {
					uni.hideLoading()
					if (res.data.code == 0) {
						uni.showToast({
							title: '提交成功',
						})
						let timer =  setTimeout(() => {
							uni.redirectTo({
								url: `/packageA/pages/writeRecord/writeRecord?module=${this.module}`
							})
							timer = null
						}, 1000)
					}
					console.log(res)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import 'lawsuitWrite.scss'
</style>